<?php
namespace app\controller\api;

use app\service\AppViewService;
use share\controller\ApiBaseController;
use app\validate\AppViewValidate;
use share\service\ContextService;

class AppViewController extends ApiBaseController{

    protected $beforeActionList = [
        // "checkSign",    
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){
        $this->validate = new AppViewValidate();
    }

    /**
     * 适配最适合的视图模块信息
     */
    public function match_action(){    
        $param = $this->request->param();
        if(!isset($param['package'])){
            $param['package'] = ContextService::getPackageName();
        }   
        if(!isset($param['vendor'])){
            $param['vendor'] = ContextService::getVendor();
        }   
        if(!isset($param['clientType'])){
            $clientType = ContextService::getClientType();
            if(strtolower($clientType) !== 'ios'){
                $param['clientType'] = 1;
            }
            else{
                $param['clientType'] = 2;
            }
        } 
        if(!isset($param['version'])){
            $param['version'] = $this->request->header('version');
        } 
        $input = $this->check('match',$param);      
        $res = AppViewService::matchModule($input);
        if($res){
            if(strlen($res['module'])){
                $res['module'] = @json_decode($res['module'],true);
            }
        }
        else{
            $res = [
                'module' => [],
            ];
        }
        // // oss
        // $ossIndex = array_search('oss',$res['module']);
        // if($ossIndex !== false){
        //     array_splice($res['module'],$ossIndex,1);
        // }
        return self::ok($res);
    }

}
